package com.qf.service;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.List;

/**
 * author:z
 * time:2022/1/8 17:02
 * description:
 */
@Service
public class WordService {

    private  XWPFDocument doc = new XWPFDocument();
    XWPFParagraph para;
    XWPFRun run;


    public  ResponseEntity<byte[]>  ExportInterpretationSignsWordById(int id) throws Exception {

        //创建表格
        XWPFTable table  = doc.createTable(8,4);
        table.setCellMargins(3, 5, 3, 5);
//	        table.addNewCol();//添加新列
//	        table.createRow();//添加新行
        String[] title1 = new String[]{"编号"," ","项目类型"," "};
        String[] title2 = new String[]{"调查日期"," ","建设状态"," "};
        XWPFTableRow row;
        XWPFTableCell cell;
        CTTcPr cellPr;
        for(int j=0;j<8;j++){
            row = table.getRow(j);

            row.setHeight(600);

            //第一行
            for(int i=0;i<title1.length;i++){
                //创建单元格
                cell = row.getCell(i);
                //单元格属性
                cellPr = cell.getCTTc().addNewTcPr();
                //设置宽度
                cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                //垂直居中
                cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                //第一个段落
                para = cell.getParagraphs().get(0);
                //设置对齐方式
                para.setAlignment(ParagraphAlignment.CENTER);
                //段落的对齐方式 1左 2中 3右 4往上 左 不可写0和负数
                para.setFontAlignment(2);
                //一个XWPFRun代表具有相同属性的一个区域：一段文本
                run = para.createRun();
                //设置字体
                run.setFontFamily("仿宋");
                //设置字体大小
                run.setFontSize(13);
                if(j==0) {
                    //字体宽
//	                    run.setBold(true);

                    if(i==1) {
//                        title1[i]=temp.getXmnumber();
                        run = para.createRun();
                        run.setFontSize(11);
                        run.setText(title1[i]);
                    }
                    if(i==3) {
//                        title1[i]=temp.getXmtype();
                        run = para.createRun();
                        run.setFontSize(11);
                        run.setText(title1[i]);
                    }

                    if(i==2) {
                        run.setText(title1[i]);
                    }
                    if(i==0) {
                        run.setText(title1[i]);
                    }

                }
            }
            //第二行

            if(j==1){
                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(0);
                        cellPr = cell.getCTTc().addNewTcPr();
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        para = cell.getParagraphs().get(0);
                        para.setAlignment(ParagraphAlignment.CENTER);
                        para.setFontAlignment(2);
                        run = para.createRun();
                        run.setFontFamily("仿宋");
                        run.setFontSize(13);
                        run.setText("扰动类型");
                    }
                    if(y==1) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//                        cell.setText(temp.getXmexcitation());
                    }else {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }


                }

            }
            if(j==2){
                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(0);
                        cellPr = cell.getCTTc().addNewTcPr();
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        para = cell.getParagraphs().get(0);
                        para.setAlignment(ParagraphAlignment.CENTER);
                        para.setFontAlignment(2);
                        run = para.createRun();
                        run.setFontFamily("仿宋");
                        run.setFontSize(13);
                        run.setText("项目名称");
                    }
                    if(y==1) {
                        cell = row.getCell(y);

                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//                        cell.setText(temp.getXmname());
                    }else {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                }


            }
            for(int i=0;i<title2.length;i++){
                cell = row.getCell(i);
                cellPr = cell.getCTTc().addNewTcPr();
                cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                para = cell.getParagraphs().get(0);
                para.setAlignment(ParagraphAlignment.CENTER);
                para.setFontAlignment(2);
                run = para.createRun();
                run.setFontFamily("仿宋");
                run.setFontSize(13);
                if(j==3) {
                    if(i==1) {
//                        title2[i]=CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXminvestigationdate())) ;
                        run = para.createRun();
                        run.setFontSize(11);
                        run.setText(title2[i]);
                    }
                    if(i==3){
//                        title2[i]=temp.getXmstate();
                        run = para.createRun();
                        run.setFontSize(11);
                        run.setText(title2[i]);
                    }
                    if(i==2) {
                        run.setText(title2[i]);
                    }
                    if(i==0) {
                        run.setText(title2[i]);
                    }

                }
            }
            if(j==4){
                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(0);
                        //单元格属性
                        cellPr = cell.getCTTc().addNewTcPr();
                        //设置宽度
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        //第一个段落
                        para = cell.getParagraphs().get(0);

                        //设置对齐方式
                        para.setAlignment(ParagraphAlignment.CENTER);
                        //居中
                        para.setFontAlignment(2);

                        run = para.createRun();
                        run.setFontFamily("仿宋");
                        run.setFontSize(13);
                        run.setText("详细地址");
                    }
                    if(y==1) {
                        cell = row.getCell(y);

                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//                        cell.setText(temp.getXmdetailaddress());
                    }else {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                }

            }
            if(j==5){
                //String imgPath="E:\\design_file_info_path\\设计文件\\2.jpg";

                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                        List<XWPFParagraph> paragraphs = cell.getParagraphs();
                        XWPFParagraph newPara = paragraphs.get(0);
                        XWPFRun imageCellRunn = newPara.createRun();
//                        System.out.println(temp.getXmimgpath()+ File.separator+temp.getXmimgname()+"图片路径");
//                        File image = new File(temp.getXmimgpath()+File.separator+temp.getXmimgname());
//                        if(!image.exists()){
//                            continue;
//                        }
//                        try (FileInputStream is = new FileInputStream(temp.getXmimgpath()+File.separator+temp.getXmimgname())) {
//                            imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
                    }
                    if(y==1) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);

                    }
                    if(y==2) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);

                        cellPr = cell.getCTTc().addNewTcPr();
                        //设置宽度
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        //第一个段落
                        para = cell.getParagraphs().get(0);

                        //设置对齐方式
                        para.setAlignment(ParagraphAlignment.LEFT);
                        //居左
                        para.setFontAlignment(1);
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
                        cell.setText("（1）照片拍摄位置经纬度:");
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("    经度:"+temp.getXmlngdu()+"°"+temp.getXmlngfen()+"′"+temp.getXmlngmiao()+"″");
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("    纬度:"+temp.getXmlatdu()+"°"+temp.getXmlatfen()+"′"+temp.getXmlatmiao()+"″");
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（2）照片拍摄时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmpictime())));
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
                        cell.setText(null);

                    }
                    if(y==3) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);

                    }

                }

            }
            if(j==6){
                //String imgPath="E:\\design_file_info_path\\设计文件\\3.jpg";
                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                        List<XWPFParagraph> paragraphs = cell.getParagraphs();
                        XWPFParagraph newPara = paragraphs.get(0);
                        XWPFRun imageCellRunn = newPara.createRun();
//                        File image = new File(temp.getXmmiddleaddress()+File.separator+temp.getXmmiddlepicname());
//                        if(!image.exists()){
//                            continue;
//                        }
//                        try (FileInputStream is = new FileInputStream(temp.getXmmiddleaddress()+File.separator+temp.getXmmiddlepicname())) {
//                            imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
                    }
                    if(y==1) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                    if(y==2) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);

                        cellPr = cell.getCTTc().addNewTcPr();
                        //设置宽度
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        //第一个段落
                        para = cell.getParagraphs().get(0);

                        //设置对齐方式
                        para.setAlignment(ParagraphAlignment.LEFT);
                        //居左
                        para.setFontAlignment(1);
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（1）中分辨率影像类型:"+temp.getXmmiddletype());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（2）中分辨率影像成像时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmmiddletime())));
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（3）中分辨率影像空间分辨率:"+temp.getXmmiddleresolution());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（4）中分辨率影像特征:"+temp.getXmmiddletype());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
                        cell.setText(null);
                    }
                    if(y==3) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                }
            }
            if(j==7){
                //	String imgPath="E:\\design_file_info_path\\设计文件\\4.jpg";

                for(int y=0;y<4;y++){
                    if(y==0) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                        List<XWPFParagraph> paragraphs = cell.getParagraphs();
                        XWPFParagraph newPara = paragraphs.get(0);
                        XWPFRun imageCellRunn = newPara.createRun();
//                        System.out.println(temp.getXmhighaddress()+File.separator+temp.getXmhighpicname());
//                        File image = new File(temp.getXmhighaddress(),temp.getXmhighpicname());
//                        System.out.println(image.getAbsolutePath());
//                        if(!image.exists()){
//                            continue;
//                        }
//                        try (FileInputStream is = new FileInputStream(image)) {
//
//                            imageCellRunn.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, image.getName(), Units.toEMU(160), Units.toEMU(160)); // 200x200 pixels
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
                    }
                    if(y==1) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                    if(y==2) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);

                        cellPr = cell.getCTTc().addNewTcPr();
                        //设置宽度
                        cellPr.addNewTcW().setW(BigInteger.valueOf(5000));
                        //第一个段落
                        para = cell.getParagraphs().get(0);

                        //设置对齐方式
                        para.setAlignment(ParagraphAlignment.LEFT);
                        //居左
                        para.setFontAlignment(1);
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（1）高分辨率影像类型:"+temp.getXmhightype());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（2）高分辨率影像成像时间:"+CommonUtil.transformDate(CommonUtil.getSysDate(temp.getXmhightime())));
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（3）高分辨率影像空间分辨率:"+temp.getXmhighresolution());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
//                        cell.setText("（4）高分辨率影像特征:"+temp.getXmhighfeature());
                        run = para.createRun();
                        run.addBreak();
                        run.addBreak();
                        cell.setText(null);
                    }
                    if(y==3) {
                        cell = row.getCell(y);
                        cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    }
                }
            }
        }
        return null;
    }
    public void buildExcelFile(String filename) throws Exception {
        FileOutputStream fos = new FileOutputStream(filename);
        doc.write(fos);
        fos.flush();
        fos.close();
    }

    public void buildWordDocument(String filename, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        doc.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}
